#include "application.h"

extern INT8U	g_thumbnail_reply_action_flag;

//	prototypes
void			state_thumbnail_init(void);
void			state_thumbnail_exit(void);

void state_thumbnail_init(void)
{
	DBG_PRINT("%s ... \r\n", __func__);
	DBG_PRINT("Thumbnail state init enter\r\n");
	ap_thumbnail_init(0);
}

void state_thumbnail_entry(void *para)
{
	EXIT_FLAG_ENUM	exit_flag = EXIT_RESUME;
	INT32U			msg_id;
	INT16U			play_index;
	STAudioConfirm	*audio_temp;

	state_thumbnail_init();

	DBG_PRINT("%s ... \r\n", __func__);

	while(exit_flag == EXIT_RESUME)
	{
		if(msgQReceive(ApQ, &msg_id, (void *) ApQ_para, AP_QUEUE_MSG_MAX_LEN) == STATUS_FAIL)
		{
			continue;
		}

		switch(msg_id)
		{
		case EVENT_APQ_ERR_MSG:
			audio_temp = (STAudioConfirm *) ApQ_para;
			if((audio_temp->result == AUDIO_ERR_DEC_FINISH) && (audio_temp->source_type == AUDIO_SRC_TYPE_APP_RS))
			{
				//gpio_write_io(SPEAKER_EN_PIN, DATA_LOW);
			}
			else
			{
				audio_confirm_handler((STAudioConfirm *) ApQ_para);
			}

			break;

		case MSG_STORAGE_SERVICE_MOUNT:
			ap_state_handling_storage_id_set(ApQ_para[0]);
			ap_state_handling_str_draw_exit();
			ap_thumbnail_exit();
			ap_thumbnail_init(1);
			DBG_PRINT("[State Thumbnail Mount OK]\r\n");
			break;

		case MSG_STORAGE_SERVICE_NO_STORAGE:
			g_thumbnail_reply_action_flag = 1;
			ap_state_handling_storage_id_set(ApQ_para[0]);
			ap_thumbnail_sts_set(THUMBNAIL_UNMOUNT);
			ap_thumbnail_stop_handle();
			ap_thumbnail_no_media_show(STR_NO_SD);
			DBG_PRINT("[State Thumbnail Mount FAIL]\r\n");
			break;

		case MSG_APQ_POWER_KEY_ACTIVE:
		case MSG_APQ_SW_RESET:
			switch(msg_id)
			{
				case MSG_APQ_POWER_KEY_ACTIVE:
					ap_state_handling_power_off(0);
				break;
				case MSG_APQ_SW_RESET:
					ap_state_handling_power_off(1);
				break;
			}
			break;

		case MSG_APQ_MODE:
			if(!g_thumbnail_reply_action_flag)
			{
				OSTimeDly(1);
				msgQSend(ApQ, msg_id, NULL, 0, MSG_PRI_NORMAL);
				break;
			}

			OSQPost(StateHandlingQ, (void *) STATE_VIDEO_RECORD);
			exit_flag = EXIT_BREAK;
			break;

		case MSG_APQ_FUNCTION_KEY_ACTIVE:
			if(!g_thumbnail_reply_action_flag)
			{
				OSTimeDly(1);
				msgQSend(ApQ, msg_id, NULL, 0, MSG_PRI_NORMAL);
				break;
			}

			play_index = ap_thumbnail_func_key_active();
			OSQPost(StateHandlingQ, (void *) (STATE_BROWSE | (play_index << 16)));
			exit_flag = EXIT_BREAK;
			break;

		case MSG_STORAGE_SERVICE_BROWSE_REPLY:
			ap_thumbnail_reply_action((STOR_SERV_PLAYINFO *) ApQ_para);
			break;

		case MSG_APQ_NEXT_KEY_ACTIVE:
			if(!g_thumbnail_reply_action_flag)
			{
				OSTimeDly(1);
				msgQSend(ApQ, msg_id, &ApQ_para[0], sizeof(INT8U), MSG_PRI_NORMAL);
				break;
			}

			ap_thumbnail_next_key_active(ApQ_para[0]);
			break;

		case MSG_APQ_PREV_KEY_ACTIVE:
			if(!g_thumbnail_reply_action_flag)
			{
				OSTimeDly(1);
				msgQSend(ApQ, msg_id, &ApQ_para[0], sizeof(INT8U), MSG_PRI_NORMAL);
				break;
			}

			ap_thumbnail_prev_key_active(ApQ_para[0]);
			break;

		case MSG_APQ_CONNECT_TO_PC:
			if(!g_thumbnail_reply_action_flag)
			{
				OSTimeDly(1);
				msgQSend(ApQ, msg_id, &ApQ_para[0], sizeof(INT8U), MSG_PRI_NORMAL);
				break;
			}

			play_index = ap_thumbnail_func_key_active();
			OSQPost(StateHandlingQ, (void *) (STATE_BROWSE | (play_index << 16)));
			exit_flag = EXIT_BREAK;
			break;

#if C_SCREEN_SAVER == CUSTOM_ON

		case MSG_APQ_KEY_IDLE:
			ap_state_handling_lcd_backlight_switch(0);
			break;

		case MSG_APQ_KEY_WAKE_UP:
			ap_state_handling_lcd_backlight_switch(1);
			break;
#endif

		case MSG_APQ_MENU_KEY_ACTIVE:
			break;

#if C_BATTERY_DETECT == CUSTOM_ON

		case MSG_APQ_BATTERY_LOW_SHOW:
			//ap_state_handling_clear_all_icon();
			OSTimeDly(1);
			ap_state_handling_str_draw(STR_BATTERY_LOW, WARN_STR_COLOR);
			msgQSend(PeripheralTaskQ, MSG_PERIPHERAL_TASK_BATTERY_LOW_BLINK_START, NULL, 0, MSG_PRI_NORMAL);
			break;
#endif

		case MSG_APQ_USER_CONFIG_STORE:
			ap_state_config_store();
			break;

		#if defined(C_FIRMWARE_UPGRADE) && C_FIRMWARE_UPGRADE
		case MSG_STORAGE_FIRMWARE_UPGRADE:
			OSQPost(StateHandlingQ, (void *) STATE_FIRMWARE_UPGRADE);
			exit_flag = EXIT_BREAK;
			break;
		#endif // defined(C_FIRMWARE_UPGRADE) && C_FIRMWARE_UPGRADE

		default:
			ap_state_common_handling(msg_id);
			break;
		}
	}

	state_thumbnail_exit();
	if(msg_id == MSG_APQ_CONNECT_TO_PC)
	{
		msgQFlush(ApQ);
		msgQSend(ApQ, MSG_APQ_CONNECT_TO_PC, NULL, 0, MSG_PRI_NORMAL);
	}
	else
	if(msg_id == MSG_APQ_MODE)
	{
		OSTimeDly(1);
	}
}

void state_thumbnail_exit(void)
{
	ap_thumbnail_exit();
	ap_state_handling_str_draw_exit();
	DBG_PRINT("Exit Thumbnail state\r\n");
}
